package com.util.xls;

import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

public class ArrayUtils {

	public static String getWhereLeftLikeValue(String value) {
		return new StringBuilder().append("'").append(Optional.ofNullable(value).orElse("")).append("%'").toString();
	}

	public static String getWhereLikeValue(String value)
	{
		return new StringBuilder().append("'%").append(Optional.ofNullable(value).orElse("")).append("%'").toString();
	}

	public static String getWhereValue(String value)
	{
		return new StringBuilder().append("'").append(Optional.ofNullable(value).orElse("")).append("'").toString();
	}

	public static String getWhereValue(LocalDateTime value)
	{
		if (Objects.isNull(value)) return "";
		return "'"+value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))+"'";
	}


	public static String getWhereValue(Long value)
	{
		if (Objects.isNull(value)) return "";
		return String.valueOf(value);
	}

	public static String getWhereValue(Integer value)
	{
		if (Objects.isNull(value)) return "";
		return String.valueOf(value);
	}

	public static boolean isNullOrZero(Long i)
	{
		if (Objects.nonNull(i))
			return 0==i.intValue();
		return true;
	}

	public static boolean isNullOrZero(Integer i)
	{
		if (Objects.nonNull(i))
			return 0==i.intValue();
		return true;
	}


	public static boolean isBigThanOrEqZero(Integer i)
	{
		if (!isNullOrZero(i))
			return i.intValue()>=0;

		return false;
	}

	public static boolean isBigThanZero(Long i)
	{
		if (!isNullOrZero(i))
			return i.intValue()>0;

		return false;
	}

	public static boolean isBigThanZero(Integer i)
	{
		if (!isNullOrZero(i))
			return i.intValue()>0;

		return false;
	}

	
	public static <T> boolean isEmpty(T [] array) {
		if(array==null) {
			return true;
		}else if(array.length<=0) {
			return true;
		}
		return false;
	}
	
	public static <T> boolean isNotEmpty(T [] array) {
		return !isEmpty(array);
	}

	public static <K, V> boolean isNotEmpty(Map<K, V> map) {
		if (map == null)
			return false;
		if (map.isEmpty())
			return false;
		return true;
	}
	
	public static <K, V> boolean isEmpty(Map<K, V> map) {
		return !isNotEmpty(map);
	}

	public static <T> boolean isEmpty(Collection<T> list) {
		if (list == null)
			return true;
		if (list.isEmpty())
			return true;
		return false;
	}

	public static <T> boolean isNotEmpty(Collection<T> list) {
		return !isEmpty(list);
	}

	public static <T> T getEntity(Function<T, T> function) {
		return function.apply(null);
	}

	public static <T> String join(Collection<T> list, String splitter) {
		if (ArrayUtils.isEmpty(list)) {
			return null;
		}
		StringBuilder builder = new StringBuilder();
		for (T item : list) {
			if (builder.length() != 0) {
				builder.append(splitter);
			}
			if (item != null) {
				builder.append(item);
			}
		}
		return builder.toString();
	}

	public static int[] listToArray(Collection<Integer> list) {
		int[] array = new int[list.size()];
		int count = 0;
		for (Integer integer : list) {
			array[count] = integer;
			count++;
		}
		return array;
	}

	public static String getWhereDateValue(LocalDateTime localDateTime) {
		Assert.notNull(localDateTime,"作为查询条件的日期不能为空");
		return new StringBuilder().append("'").append(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))).append("'").toString();
	}

	public static String getWhereDateTimeValue(LocalDateTime localDateTime) {
		Assert.notNull(localDateTime,"作为查询条件的日期不能为空");
		return new StringBuilder().append("'").append(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).append("'").toString();
	}

	public static String getWhereDateValueStart(LocalDateTime localDateTime) {
		Assert.notNull(localDateTime,"作为查询条件的日期不能为空");
		return new StringBuilder().append("'").append(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00"))).append("'").toString();
	}

	public static String getWhereDateValueEnd(LocalDateTime localDateTime) {
		Assert.notNull(localDateTime,"作为查询条件的日期不能为空");
		localDateTime = localDateTime.plusDays(1);
		return new StringBuilder().append("'").append(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00"))).append("'").toString();
	}


//	public static String getInValue(EnumSet<ResponseResultEnum> enumSet) {
//		Assert.isTrue(Objects.nonNull(enumSet)&& !enumSet.isEmpty(),"枚举集合不能为空");
//		return enumSet.stream().map(item->String.valueOf(item.ordinal())).collect(Collectors.joining(",","(",")"));
//	}
}